#include "Host.h"

AgoraCore::Host::Host()
{
}

AgoraCore::Host::~Host()
{
}

unsigned int	AgoraCore::Host::Init(unsigned short int port, const std::string& address)
{
  unsigned int	error;
  
  if ((error = this->CreateSocket()) != 0)
    return (error);
  return (this->BindSocket(port, address));
}

unsigned int	AgoraCore::Host::Listen(unsigned int queueLength)
{
  if (listen(this->_socket, queueLength) != 0)
    return (1);
  return (0);
}

unsigned int	AgoraCore::Host::Accept(Connection** incomingConnection)
{
#ifdef _WIN32
  SOCKET		socket;
  SOCKADDR_IN	socketInfos;
#else
  unsigned int	socket;
  sockaddr_in		socketInfos;
#endif
  int				socketInfosLength = sizeof(socketInfos);
  
#ifdef _WIN32
  socket = accept(this->_socket, (SOCKADDR*)&socketInfos, &socketInfosLength);
#else
  socket = accept(this->_socket, (sockaddr*)&socketInfos, (socklen_t*)&socketInfosLength);
#endif
  if (socket < 0)
    {
      return (1);
    }
  *incomingConnection = new Connection(inet_ntoa(socketInfos.sin_addr), ntohs(socketInfos.sin_port), socket, socketInfos);
  return (0);
}
